<?php

namespace app\admin\model\staff;

use think\Db;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class Staffclass extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'staff_class';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'scheduletime_text',
        'starttime_text',
        'endtime_text',
        'status_text'
    ];

    
    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }


    public function getScheduletimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['scheduletime']) ? $data['scheduletime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setScheduletimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function staff()
    {
        return $this->belongsTo('Staff', 'staff_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public static function init()
    {
        self::beforeWrite(function ($row) {
            $starttime = $row['starttime'];
            $endtime = $row['endtime'];
            if ($starttime === '' || $endtime === '') {
                throw new Exception("开始时间和结束时间不正确");
            }
            if ($starttime > $endtime) {
                throw new Exception("开始时间不能大于结束时间");
            }
        });
    }

    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['starttime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['endtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }



    public function getRenderAttr($value, $data)
    {
        $allDay = ($data['starttime'] === $data['endtime'] && date("H:i:s", $data['starttime']) == '00:00:00');
        $staff = Db::name('staff')->where('id', $data['staff_id'])->find();
        $Data = Db::name('classes')->field('name,id,starttime,endtime')->where(['deletetime'=>null,'status'=>'normal'])->select();
        foreach ($Data as $k => &$v) {
           $v['staff_class'] = Db::name('staff_class')->alias('s')->field('s.*,staff.name as staff_name')
               ->join('staff','s.staff_id = staff.id','left')
               ->where(['deletetime'=>null,'s.status'=>'normal','classes_id'=>$v['id']])
               ->select();
        }

        $staff_name = $staff['name'] ?? '';
        return [
            'id'              => $data['id'],
            'staff_id'        => $data['staff_id'],
            'classes_id'        => $data['classes_id'],
            'title'           => $staff_name,
            'start'           => date("c", $data['starttime']),
            'end'             => date("c", $data['endtime']),
            'backgroundColor' => "{$data['background']}",
            'borderColor'     => "{$data['background']}",
            'allDay'          => $allDay,
            'data'             => $Data,
        ];
    }

    protected function setStarttimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setEndtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
